$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/siteBannerManage/list',
        datatype: "json",
        colModel: [
            { label: '图片', name: 'picUrl', width: 80,sortable:false, formatter: function(value, options, row){

                return    value==undefined || value==null || value=="" ? '<image src="/statics/img/noImage.png" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>' : '<image src="'+value+'" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>';
            }},
            { label: '用户ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true},
            { label: '图片地址', name: 'picUrl', width: 80 },
            { label: '更新时间', name: 'updateTime', index:"updateTime",width: 80, sortable:false},

        ],
        viewrecords: true,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});


function uploadImage(id,name)
{

    $(id).fileupload({
        url: baseURL + "fileUpload/images",
        type: "POST",
        autoUpload: true,
        sequentialUploads: true,
        singleFileUploads: false,
    }).bind('fileuploadprogress', function (e, data) {

        var tempArray=[];
        vm.picUrl=tempArray.join(",");

    }).bind('fileuploaddone', function (e, data) {

        if(data.result.errcode==0){
                vm.siteBanner.picUrl = data.result.data.join(",");
            alert("上传成功！");
        }else{
            alert("上传失败！");
        }

    });


}





var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            title: null
        },
        requestUrl:baseURL+"fileUpload/image",
        showList: true,
        title:null,
        siteBanner:{
            status:0
        },
        token:{token:localStorage.getItem("token")},
        picUrl:""
    },
    methods: {
        handleRemove0(file, fileList) {
            vm.picUrl="";
        },
        handlePreview0(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 5;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 5MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess0(res, file) {
            vm.picUrl=res.data;
        },
        query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.siteBanner = {status:0,picUrl:""};
            vm.picUrl="";
        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userId);

        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "orth/siteBannerManage/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {

            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            /*if(vm.validator()){
                return ;
            }*/

                vm.siteBanner.picUrl=vm.picUrl;
                if(vm.siteBanner.picUrl=="")
                {
                    alert("请上传图片");
                    return;
                }

            var url =  "orth/siteBannerManage/save" ;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.siteBanner),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(id){
            $.get(baseURL + "orth/siteBannerManage/info/"+id, function(r){
                vm.siteBanner = r.siteBanner;
                vm.picUrl=vm.siteBanner.picUrl;
            });
        },
        reload: function () {
            vm.showList = true;
            $(".formError").hide();
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'test': vm.q.title},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {

        }
    }
});